package com.yeti.controller;

import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yeti.domain.Cliente;
import com.yeti.domain.Lista;
import com.yeti.service.ClienteService;
import com.yeti.service.ListaService;

@Controller @RequestMapping(value="/cliente")
public class ClienteController {
	
	@Autowired
	ClienteService clienteService;
	
	@Autowired
	ListaService listaService;
	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping(value="/home", method = RequestMethod.GET)
	public String Home(ModelMap model, Principal principal,HttpServletRequest request ) {
 
		String name = principal.getName();
		String pageString = request.getParameter("page");
		int page = 1;
		if(pageString!=null){
			page = Integer.parseInt(pageString);
		}
		ArrayList<Object> listas = listaService.getListas(name,page);
		model.addAttribute("username", name);
		model.addAttribute("listas", listas.get(0));
		model.addAttribute("page",page);
		model.addAttribute("totalPages", (Integer)listas.get(1));	
		
		return "/cliente/show";
	}
	
	@RequestMapping(value="/createList", method = RequestMethod.GET)
	public String addLista(ModelMap model, Principal principal ) {
		
		String name = principal.getName();
		model.addAttribute("username", name);		
		
		return "/cliente/newList";
	}
	
	@RequestMapping(value="/crearLista", method = RequestMethod.GET)
	public void crearLista(ModelMap model,HttpServletRequest request, HttpServletResponse response, Principal principal) {	
		listaService.addLista(request.getParameter("titulo"),principal.getName());
		String name = principal.getName();
		model.addAttribute("username", name);
		try {
			response.sendRedirect("home");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/eliminarLista", method = RequestMethod.POST)
	public @ResponseBody String eliminarLista(HttpServletRequest request, Principal principal) {
		String name = principal.getName();
		int id = Integer.parseInt(request.getParameter("id"));
		listaService.removeLista(id,name);
		return "";
	}
	
	@RequestMapping(value="/editar/{idLista}/addProducto", method = RequestMethod.POST)
	public void addProducto(@PathVariable("idLista") int idLista, HttpServletRequest request, HttpServletResponse response, Principal principal) {
		String nombre = request.getParameter("nombre");
		int cantidad = Integer.parseInt(request.getParameter("cantidad"));
		float precio = Float.parseFloat(request.getParameter("precio"));
		
		listaService.addProducto(idLista, nombre, cantidad, precio);
		try {
			response.sendRedirect("/trato/cliente/editar/"+idLista);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/editar/{idLista}/updateProducto", method = RequestMethod.POST)
	public void updateProducto(@PathVariable("idLista") int idLista, HttpServletRequest request, HttpServletResponse response, Principal principal) {
		String nombre = request.getParameter("nombre");
		int idProducto = Integer.parseInt(request.getParameter("idProducto"));
		int cantidad = Integer.parseInt(request.getParameter("cantidad"));
		float precio = Float.parseFloat(request.getParameter("precio"));
		listaService.updateProducto(idLista, idProducto, nombre, cantidad, precio);
	}
	
	@RequestMapping(value="/editar/{idLista}/deleteProducto", method = RequestMethod.POST)
	public void deleteProducto(@PathVariable("idLista") int idLista, HttpServletRequest request, HttpServletResponse response, Principal principal) {
		int idProducto = Integer.parseInt(request.getParameter("idProducto"));
		
		listaService.removeProducto(idLista, idProducto);
		try {
			response.sendRedirect("/trato/cliente/editar/"+idLista);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/editar/{idLista}/deleteAmigo", method = RequestMethod.POST)
	public void deleteAmigo(@PathVariable("idLista") int idLista, HttpServletRequest request, HttpServletResponse response, Principal principal) {
		int idAmigo = Integer.parseInt(request.getParameter("idAmigo"));
		listaService.removeAmigo(idLista, idAmigo);
		try {
			response.sendRedirect("/trato/cliente/editar/"+idLista);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/editar/{idLista}", method = RequestMethod.GET)
	public String editarLista(@PathVariable("idLista") int listaId , HttpServletResponse response,ModelMap model, Principal principal ) {
		List<Cliente> clientes = listaService.getListaAmigosSeleccionables(listaId);
		Lista lista = listaService.getLista(listaId);
		String name = principal.getName();
		
		List<Cliente> listaAmigos = listaService.getAmigosInLista(listaId, name);
		
		model.addAttribute("listaAmigos", listaAmigos);
		model.addAttribute("clientes", clientes);
		model.addAttribute("lista", lista);
		model.addAttribute("username", name);
		
		return "/cliente/editList";
	}
	
	@RequestMapping(value="/editar/{idLista}/guardarLista", method = RequestMethod.POST)
	public void guardarLista(@PathVariable("idLista") int listaId , HttpServletRequest request,	HttpServletResponse response, ModelMap model, Principal principal ) {
		listaService.updateLista(listaId, request.getParameter("nombreLista"));
	}
	
	@RequestMapping(value="/editar/{idLista}/addAmigo", method = RequestMethod.GET)
	public void addAmigo(@PathVariable("idLista") int listaId , HttpServletRequest request,	HttpServletResponse response, ModelMap model, Principal principal ) {
//		if(request.getParameter("clienteCombo") == null){
//			try {
//				
//				response.sendRedirect("/trato/cliente/editar/" + listaId);
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
		
		int idAmigo = Integer.parseInt(request.getParameter("clienteCombo"));
		
		boolean correcto = listaService.addAmigo(listaId, idAmigo);
		System.out.println(correcto);
		
		try {
			response.sendRedirect("/trato/cliente/editar/" + listaId);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
